package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by CJJ on 2017/3/2.
 *
 *
 * 我们定义一个ViewGroup，内部可以传入0到4个childView，分别依次显示在左上角，右上角，左下角，右下角
 *
 */

public class CustomViewGroup  extends ViewGroup{

    private int mWidth,mHeight;//宽，高


    public CustomViewGroup(Context context) {
        super(context);
    }

    public CustomViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        /**
         * 获得此ViewGroup上级容器为其推荐的宽，以及计算模式
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (widthMode){
            case MeasureSpec.EXACTLY:
                mWidth = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                //wrap_content时，遍历所得childview，来计算宽度

                int childCount = getChildCount(); //子view的个数

                //上面2个childview的宽度
                int topTotalWidth=0 ;
                //下面2个childview的宽度，最终和topTotalWidth对比，取大的值
                int bottomTotalWidth=0;

                for(int i=0; i<childCount; i++){
                    View childview = getChildAt(i);
                    int cWidth = childview.getMeasuredWidth();
                    MarginLayoutParams cParams = (MarginLayoutParams) childview.getLayoutParams();

                    //计算上面2个childview的宽度
                    if(i==0 || i==1){
                        topTotalWidth +=cWidth + cParams.leftMargin + cParams.rightMargin;
                    }

                    //计算下面2个childview的宽度
                    if(i==2 || i==3 ){
                        bottomTotalWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
                    }

                }

                //取二者的大值
                mWidth = Math.max(topTotalWidth,bottomTotalWidth);
                break;
        }



        /**
         * 获得此ViewGroup上级容器为其推荐的高，以及计算模式
         */
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (heightMode){
            case MeasureSpec.EXACTLY:
                mHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                //wrap_content时，遍历所得childview，来计算高度

                int childCount = getChildCount(); //子view的个数
                //左边2个childview的高度
                int leftTotalHeight=0 ;
                //右边2个childview的高度，最终和leftTotalHeight对比，取大的值
                int rightTotalHeight=0;

                for(int i=0; i<childCount; i++){
                    View childview = getChildAt(i);
                    int cHeight = childview.getMeasuredHeight();
                    MarginLayoutParams cParams = (MarginLayoutParams) childview.getLayoutParams();

                    //计算左边2个childview的高度
                    if(i==0 || i==2){
                        leftTotalHeight +=cHeight + cParams.topMargin + cParams.bottomMargin;
                    }

                    //计算右边2个childview的高度
                    if(i==1 || i==3 ){
                        rightTotalHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
                    }

                }

                //取大的值
                mHeight = Math.max(leftTotalHeight,rightTotalHeight);
                break;
        }


        setMeasuredDimension(mWidth,mHeight);
    }




    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //遍历所有childview，依次放置在四个角上
        for(int i=0 ; i<getChildCount(); i++){
            View childView = getChildAt(i);
            int cWidth = childView.getMeasuredWidth();
            int cHeight = childView.getMeasuredHeight();
            MarginLayoutParams marginLayoutParams = (MarginLayoutParams) childView.getLayoutParams();

            int cLeft=0,cTop=0,cRight=0,cBottom=0;
            switch (i){
                case 0:
                    //左上角
                    cLeft = marginLayoutParams.leftMargin;
                    cTop = marginLayoutParams.topMargin;
                    break;
                case 1:
                    //右上角
                    cLeft = mWidth - cWidth - marginLayoutParams.rightMargin;
                    cTop = marginLayoutParams.topMargin;
                    break;
                case 2:
                    //左下角
                    cLeft = marginLayoutParams.leftMargin;
                    cTop = mHeight - cHeight - marginLayoutParams.bottomMargin;
                    break;
                case 3:
                    //右下角
                    cLeft =  mWidth - cWidth - marginLayoutParams.rightMargin;
                    cTop =  mHeight - cHeight - marginLayoutParams.bottomMargin;
                    break;

            }

            cRight = cLeft + cWidth;
            cBottom = cTop + cHeight;

            //设置位置
            childView.layout(cLeft,cTop,cRight,cBottom);
        }


    }




}

